<!--  -->
<template>
  <div>
    <base-modal :title="title" :show.sync="visible" width="500px" class="batch-editing-dialog">
      <div class="dialog-box">
        <template>
          <div class="flex-y save-to-form-item">
            <div class="item-tips">
              {{$t('common.label.findSameProduct', {num: total})}}
            </div>
            <biz-remote-select
              ref="product"
              v-model="currData"
              :remote-method="searchData"
              :filterable="false"
              @input="updateData"
              :placeholder="$t('common.validate.searchKeywordProduct')"
              :computed-width-keys="['name', 'serialNumber']"
            >
              <div class="template-option product-template-option" slot="option" slot-scope="{ option }">
                <h3>{{ option.name }}</h3>
                <p>
                  <span>
                    <label>{{$t('common.fields.productNo.displayName')}}：</label>
                    <span>{{ option.serialNumber }}</span>
                  </span>
                  <span>
                    <label>{{$t('common.base.productType')}}：</label>
                    <span>{{ option.type }}</span>
                  </span>
                   <span>
                    <label>{{$t('common.label.customer')}}：</label>
                    <span v-if="option.customerName">{{option.customerName}}</span>
                    <span class="customer-unbind-name" v-else>{{$t('common.base.notContact')}}</span>
                  </span>
                </p>
              </div>
            </biz-remote-select>
          </div>
        </template>
      </div>
      <div slot="footer">
        <el-button @click="visible = false">{{$t('common.base.cancel')}}</el-button>
        <el-button type="primary" :loading="btnLoading" @click="confirm">{{$t('common.base.confirm')}}</el-button>
      </div>
    </base-modal>
  <!--  -->
  </div>
</template>

<script>
import {
  getProductList
} from "@src/api/productRegisterApi";
import _ from "lodash";

export default {
  name: "save-to-product",
  props: {
    id: {},
    total: Number,
    visibleProp: {
      type: Boolean,
    }
  },
  data() {
    let name = this.$t('common.label.product');
    return {
      name,
      title: this.$t('common.label.saveTo', {name: name}),
      btnLoading: false,
      currData: void 0,
    };
  },
  computed: {
    visible: {
      get() {
        return this.visibleProp;
      },
      set(val) {
        this.$emit("changeVisibleProp", val);
      }
    },
  },
  watch: {
    visible(newVal, oldVal) {
      if (newVal == false) {
        this.currData = [];
        this.btnLoading = false;
      }
    },
  },
  methods: {
    confirm() {
      if(!this.currData.length){
        return this.$message.error(`${this.$t('common.base.pleaseSelect')}${this.name}`);
      }
      this.$emit("confirm", { chooseData: this.currData[0] });
    },
    /**
     * @description 搜索
     */
    async searchData(params = {}) {
      try {
        params = {
          id: this.id,
          ...params
        }
        let result = await getProductList(params);
        result = result.data
        if (!result || !result.list) return;

        result.list = result.list.map((data) =>
          this.selectConversion(data)
        );

        return result;
      } catch (error) {
        console.warn(`${this.$options.name}: searchData -> error`, error);
      }
    },
    selectConversion(obj) {
      if(!obj) return {};
    
      let { name, id } = obj;
  
      return Object.freeze({
        label: name,
        value: id,
        ...obj
      })
    },
    updateData(value = []) {
      this.currData = value
    }
  },
};
</script>
<style lang="scss" scoped>
.dialog-box {
  padding: 9px 0 0 0;
}
.save-to-form-item {
  margin-bottom: 45px;
  .item-tips {
    margin-bottom: 18px;
  }
}
.checked-item {
  background: $color-primary;
}
.link-product{
  display: flex;
  padding: 5px;
  // border-bottom: 1px solid $color-border-l1;
  width: 100%;
  ::v-deep .el-checkbox{
    margin-right: 5px;
  }
  ul{
    padding-left: 5px;
    .product-name{
      font-size: 14px;
      color: $text-color-primary;
    }
    li{
      font-size: 12px;
      height: 22px;
      line-height: 22px;
      overflow: hidden;
      text-overflow:ellipsis;
      white-space: nowrap;
      color: $text-color-secondary;
    }
  }
}
.common-list {
  font-size: 12px;
  .common-list-name {
    font-size: 14px;
    font-weight: bold;
    color: #606266;
  }
  .common-list>* {
    color: #8C8C8C;
  }
  .customer-unbind-name{
     color:#BFBFBF;
  }
}
.theme-default .el-option {
  &.selected {
    background: #fff;
    .common-list>* {
      font-weight: bold;
      color: #666;
    }
    .common-list-name {
      color: $color-primary-light-6;
    }
    &:hover {
      background-color: $color-primary-light-1;
    }
  }
}
</style>
<style lang="scss">
.el-dialog__body {
  border-top: 1px solid rgba(0, 0, 0, 0.09);
  border-bottom: 1px solid rgba(0, 0, 0, 0.09);
  padding: 0;
}

.el-select__input{
  margin-left: 12px;
}

.el-select-dropdown__item{
  height: auto;
  // width: 360px;
}

.select-list{
  width: calc(100% - 100px);
}
.part-list{
  width: 100%;
  padding-left: 0;
  // border-bottom: 1px solid $color-border-l3;
  padding: 5px 0;
  overflow: auto;
  li{
    height: 20px;
    line-height: 20px;
  }
}

.el-select-dropdown.is-multiple .el-select-dropdown__item.selected{
  p{
    color:$color-primary;
  }
  li{
    color:$color-primary;
  }
}

.wiki-item{
  padding: 5px 0;
  width: 100%;
  // border-bottom: 1px solid $color-border-l3;
  overflow: auto;
  h4{
    margin-bottom: 0;
    height: 20px;
    line-height: 20px;
  }
  p{
    margin-bottom: 0;
    height: 20px;
    line-height: 20px;
    color: $text-color-secondary;
    font-size: 12px;

    span{
      padding: 2px 5px;
      margin-right: 6px;
      background-color: $bg-color-l3;
    }
  }
  .wiki-content{
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    word-break: break-all;
  }
}

.el-select__tags-text{
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
  white-space: normal;
  word-wrap: break-word;
  word-break: break-all;
}
.el-select .el-tag{
position: relative;
display: flex;
align-items: center;
max-width: 60%;
}
.el-select .el-select__tags-text {
  flex: 1;
}
.batch-editing-dialog {
    .base-modal-body {
      padding: 10px 20px 0;
    }

    .form-name, .form-item label {
      width: 70px;
      padding: 0;
      line-height: 32px;
    }

    .el-select {
      width: 100%;
    }

    .item {
      display: flex;
      justify-content: space-between;
      line-height: 32px;
      div {
        flex-grow: 1;
        .el-select {
          width: 100%;
        }
      }
    }

    .dialog-footer {
      display: flex;
      justify-content: flex-end;
    }

  }
  .tips-prev {
    color: #8c8c8c;
  }

  .template-option {
    * {
      margin: 0;
    }
    padding: 10px 0;
    h3 {
      font-size: 14px;
      font-weight: 500;
      line-height: 24px;
    }
    
    p {
      display: flex;
      justify-content: space-between;
      line-height: 24px;
      flex-wrap: wrap;
      flex-flow: column;
      
      & > span {
        width: 100%;
        display: flex;
        justify-content: flex-start;
        flex: 1;
        font-size: 12px;
        color: #666666;
        padding-right: 10px;
        
        & > label {
          padding: 0;
          width: 70px;
          max-width: 70px;
        }
      }
    }
  }

  .customer-template-option {
    p {
      span {
        label {
          width: 40px;
        }
      }
    }
  }

  .product-template-option { 
    h3 {
      @include text-ellipsis()
    }
    & > p {
      & > span {
        & > span {
          flex: 1;
        }
      }
      .customer-unbind-name{
        color:#BFBFBF;
      }
    }
  }
</style>

<style lang="scss">
.el-select-dropdown__empty {
  display: block !important;
}
.checkProductConfirm {
  vertical-align: baseline;
}
</style>
